package cn.tedu.boot33.controller;

import cn.tedu.boot33.entity.User;
import cn.tedu.boot33.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired(required = false)
    UserMapper mapper;

    @RequestMapping("/reg")//需要判断用户输入的是否存在,不存在则执行注册
    public int reg(@RequestBody  User user){
        User u = mapper.selectByUsername(user.getUsername());
        if (u!=null){
            return 2;//用户名已存在
        }
        mapper.insert(user);
        return 1;//注册成功
    }

    @RequestMapping("/login")//需要判断用户是否存在,密码是否正确
    public int login(@RequestBody User user){
        User u = mapper.selectByUsername(user.getUsername());
        if (u!=null){
            if (user.getPassword().equals(u.getPassword())){
                return 1;//用户存在且密码正确,登录成功
            }
            return 3;//用户存在,密码不对,
        }
        return 2;//用户不存在
    }


}
